using System.Linq;
using Net.Common;
using UnityEngine;
using UnityEngine.UI;


/****************************************************
// 功能：
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/8 23:19:50
*****************************************************/
public class RoomWindow : BaseWindow
{

    private Text time;
    private Button confirmButton;
    private Button disableButton;
    private Text readyText;
    private int timeValue = Constants.NUM_THIRTY;
    public GameObject[] left;
    public GameObject[] right;

    private void Awake()
    {
        time = FindChildTransform(transform, "time").GetComponent<Text>();
        readyText = FindChildTransform(transform, "readyText").GetComponent<Text>();
        confirmButton = FindChildTransform(transform, "confirmButton").GetComponent<Button>();
        disableButton = FindChildTransform(transform, "disableButton").GetComponent<Button>();
    }

    protected override void InitWindows()
    {
        base.InitWindows();
        
        InvokeRepeating(nameof(UpdateCountdown),0f,1f);

        confirmButton.onClick.AddListener(OnConfirmButton);
        disableButton.enabled = false;
        
        RoomController.Instance.roomWindow = this;
        
        UpdatePlayerView();
        
    }

    protected override void ClearWindows()
    {
        base.ClearWindows();
        RoomController.Instance.roomWindow = null;

    }

    /// <summary>
    /// 玩家发送确认请求
    /// </summary>
    private void OnConfirmButton()
    {
        audioManager.PlayUIAudio("matchSureClick");
        RoomController.Instance.OnConfirmRoom();
    }

    #region 倒计时

    /// <summary>
    /// 倒计时
    /// </summary>
    private void UpdateCountdown()
    {
        timeValue--;
        SetText(time, timeValue.ToString());
        if (timeValue <= 0)
        {
            FinishCountdown();
        }
    }

    private void FinishCountdown()
    {
        CancelInvoke(nameof(UpdateCountdown));
        //Debug.Log("倒计时结束");
        
        RoomController.Instance.OnCancelRoom();
    }

    #endregion
    

    public void OnUpdateRoom(RoomResponse response)
    {
        if (model.userInfo.channelId.Equals(response.channelId))
        {
            SetActive(confirmButton.gameObject, false);
            SetActive(disableButton.gameObject);
        }

        var count = model.readyRoomPlayer.Count(t => t.ready);
        var total = model.readyRoomPlayer.Count;
        
        UpdatePlayerView();

        SetText(readyText, $"{count}/{total}已就绪");

        // 所有玩家准备完毕，进入英雄选择环节
        if (count == total)
        {
            HeroController.Instance.OnHeroSelectWindow();
        }
    }

    /// <summary>
    /// 更新玩家状态视图
    /// </summary>
    private void UpdatePlayerView()
    {
        var num = model.readyRoomPlayer.Count / 2;
        
        for (var i = 0; i < left.Length; i++)
        {
            Image image = left[i].transform.Find("image").GetComponent<Image>();
            if (i < num)
            {
                left[i].SetActive(true);
                if (model.readyRoomPlayer[i].ready)
                {
                    Image border = left[i].GetComponent<Image>();
                    SetSprite(border, "Images/match/frame_sure");
                }
                StartCoroutine(SetDownImageToLocal(image, model.readyRoomPlayer[i].avatar, "avatar"));
            }
            else
            {
                left[i].SetActive(false);
            }
        }
        
        for (var i = 0; i < right.Length; i++)
        {
            Image image = right[i].transform.Find("image").GetComponent<Image>();
            if (i < num)
            {
                right[i].SetActive(true);
                if (model.readyRoomPlayer[i + num].ready)
                {
                    Image border = right[i].GetComponent<Image>();
                    SetSprite(border, "Images/match/frame_sure");
                }
                StartCoroutine(SetDownImageToLocal(image, model.readyRoomPlayer[i + num].avatar, "avatar"));
            }
            else
            {
                right[i].SetActive(false);
            }
        }
    }
}
